package cn.nawang.ebeim.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import antlr.Utils;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.domain.Customer;
import cn.nawang.ebeim.api.domain.StorageCloud;
import cn.nawang.ebeim.api.domain.StorageServer;
import cn.nawang.ebeim.api.domain.TbStorageStrategy;
import cn.nawang.ebeim.api.service.StorageServerService;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.Util;
import cn.nawang.ebeim.api.utils.ValidateUtil;

/**
 * @ClassName: StorageServcerController
 * @Description: TODO
 * @author fanyb
 * @date Aug 18, 2015 11:43:32 AM
 * @version v1.0
 */
@RestController
public class StorageServcerController {
  private static final Logger LOG = LoggerFactory.getLogger(StorageServcerController.class);

  @Autowired
  private StorageServerService storageServerService;

  /**
   * 
   * @author fanyb
   * @Description: 根据云编号查找云存储的前端列表
   * @param storageCloudId 云编号
   * @return Map<String,Object> 云存储的前端列表
   * @date Aug 18, 201511:46:23 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/storage_server/find/storage_cloud_id", method = RequestMethod.GET)
  public Map<String, Object> findUserStorageCloudList(String storageCloudId) {
    LOG.debug("/token/storage_server/find/storage_cloud_id {}" + storageCloudId);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    map.put("list", storageServerService.findByCloudIdList(storageCloudId));
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findUserStorageCloudList {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找所有服务端前置机
   * @return Map<String,Object>
   * @date Aug 28, 20159:25:18 AM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/token/storage_server/find/all", method = RequestMethod.GET)
  public Map<String, Object> findAll() {
    LOG.debug("/token/storage_server/find/all {}");
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    map.put("list", storageServerService.findAll());
    map.put("code", Constant.CODE_SUCCESS);
    LOG.debug("findAll {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 新增storageServer
   * @param storageServer
   * @return Map<String,Object>
   * @date Oct 19, 20154:40:16 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_server/add", method = RequestMethod.POST)
  public Map<String, Object> add(StorageServer storageServer) {
    LOG.debug("/storage_server/add {}", storageServer);
    Map<String, Object> map = new HashMap<String, Object>();
    boolean valid = valid(map, storageServer);
    if (!valid) {
      return map;
    }
    boolean isSuccess = storageServerService.add(storageServer);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
      map.put("desc", "新增成功。");
    } else {
      map.put("code", Constant.CODE_ERROR);
      map.put("desc", "服务器忙，请稍后再试。");
    }
    LOG.debug("add {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 分页列表
   * @param page
   * @param request
   * @return Object
   * @date Oct 19, 20158:23:17 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_server/page_list", method = RequestMethod.GET)
  public Object findAllByPaging(Pages page, HttpServletRequest request) {
    LOG.debug("/storage_server/list {}", page);
    Map<String, Object> map = new HashMap<String, Object>();
    // 组装查询条件
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();
    // ip地址
    String host = request.getParameter("host");
    if (!StringUtils.isEmpty(host)) {
      dtovect.add(new SearchFormParams("ss.host", host, "host", SearchFormParams.STRING_TYPE));
    }
    // 云存储编号
    String name = request.getParameter("name");
    if (!StringUtils.isEmpty(name)) {
      dtovect.add(new SearchFormParams("sc.name", name, "name", SearchFormParams.STRING_TYPE));
    }
    // 内部主机地址
    String internalHost = request.getParameter("internalHost");
    if (!StringUtils.isEmpty(internalHost)) {
      dtovect.add(new SearchFormParams("ss.internalHost", internalHost, "internalHost",
          SearchFormParams.STRING_TYPE));
    }
    if (dtovect.size() > 0) {
      page.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList pagelist = storageServerService.findPageList(page);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pagelist.getObjList());
    map.put("page", pagelist.getPage());
    LOG.debug("findAllByPaging {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据id获取实体
   * @param id
   * @return Object
   * @date Oct 19, 20158:23:34 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_server/id", method = RequestMethod.GET)
  public Object findOne(String id) {
    LOG.debug("/storage_server/id {}", id);
    Map<String, Object> map = new HashMap<String, Object>();
    StorageServer storageServer = storageServerService.findOne(id);
    if (storageServer == null) {
      map.put("code", Constant.CODE_ERROR);
    } else {
      map.put("code", Constant.CODE_SUCCESS);
      map.put("obj", storageServer);
    }
    LOG.debug("findOne {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 批量删除
   * @param ids
   * @return Object
   * @date Oct 19, 20158:23:50 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_server/delete", method = RequestMethod.GET)
  public Object delete(String[] ids) {
    LOG.debug("/storage_server/delete ids" + ids);
    Map<String, Object> map = new HashMap<String, Object>();
    boolean isSuccess = storageServerService.delete(ids);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
    } else {
      map.put("code", Constant.CODE_ERROR);
    }
    LOG.debug("delete {}", map);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新
   * @param storageServer
   * @return Object
   * @date Oct 19, 20158:24:02 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/storage_server/update", method = RequestMethod.GET)
  public Object update(StorageServer storageServer) {
    LOG.info("/storage_cloud/update {}", storageServer);
    Map<String, Object> map = new HashMap<String, Object>();
    boolean valid = valid(map, storageServer);
    if (!valid) {
      return map;
    }
    boolean isSuccess = storageServerService.update(storageServer);
    if (isSuccess) {
      map.put("code", Constant.CODE_SUCCESS);
      map.put("desc", "更新成功。");
    } else {
      map.put("code", Constant.CODE_ERROR);
      map.put("desc", "服务器忙，请稍后再试。");
    }
    LOG.debug("update {}", map);
    return map;
  }

  public boolean valid(Map<String, Object> resultMap, StorageServer storageServer) {
    if (Util.isBlank(storageServer.getHost()) || Util.isBlank(storageServer.getInternalHost())) {
      resultMap.put("desc", "请输入主机地址和内部主机地址。");
      return false;
    }
    if (Util.isBlank(storageServer.getPort()) || Util.isBlank(storageServer.getInternalPort())) {
      resultMap.put("desc", "请输入端口号和内部端口号。");
      return false;
    }
    if (!Util.ipCheck(storageServer.getHost()) || !Util.ipCheck(storageServer.getInternalHost())) {
      resultMap.put("desc", "很抱歉,您输入的主机地址或内部主机地址不合法。");
      return false;
    }
    if (!Util.isPort(String.valueOf(storageServer.getPort()))
        || !Util.isPort(String.valueOf(storageServer.getInternalPort()))) {
      resultMap.put("desc", "很抱歉,您输入的主机地址或内部主机地址不合法。");
      return false;
    }

    StorageServer ss =
        storageServerService.findByIpAndPort(storageServer.getHost(), storageServer.getPort());
    if (!ValidateUtil.isNull(ss)) {
      if (ValidateUtil.isEmpty(storageServer.getId())) {
        resultMap.put("desc", "很抱歉,您输入的主机地址和端口已经存在，请勿重复添加。");
        return false;
      } else {
        if (!ss.getId().equals(storageServer.getId())) {
          resultMap.put("desc", "很抱歉,您输入的主机地址和端口已经存在，请勿重复添加。");
          return false;
        }
      }
    }
    return true;
  }
}
